package cn.fancy.sdk.pay.view;

import cn.fancy.sdk.utils.MoneyDiscountHelper;
import cn.fancy.sdk.utils.SpMmkvUtils;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.entity.GuildBaseInfo;
import cn.fancy.sdk.entity.SdkBaseInfo;
import cn.fancy.sdk.entity.DevBase.DevEnv;
import cn.fancy.sdk.pay.ChargeAbstractView;
import cn.fancy.sdk.pay.GuildPayActivity;
import cn.fancy.sdk.pay.entity.ChargeListsResultEntity;
import cn.fancy.sdk.pay.entity.CpPaymentEntity;
import cn.fancy.sdk.pay.utils.PayUtil;
import java.util.Objects;

public abstract class PayDetailView extends ChargeAbstractView {
	protected GuildPayActivity activity;

	protected View view;

	protected LinearLayout cardLayout;
	protected RelativeLayout moneyLayout;
	protected LinearLayout baseInfoLayout;
	protected LinearLayout smsChannelLayout;
	protected LinearLayout smsPayInfoLayout;
	protected LinearLayout payConfirmLayout;
	/**
	 * 充值账号
	 * */
	protected TextView username;
	protected TextView usernameVal;

	/**
	 * 充值金额输入框
	 */
	protected TextView detailMoney;

	/**
	 * 充值卡类型
	 */
	protected TextView detailCardTypeEt;
	/**
	 * 充值卡面额
	 */
	protected TextView detailMoneyTypeEt;

	/**
	 * 确定
	 */
	protected Button detailConfirmBt;

	/**
	 * 用户协议
	 */
	protected LinearLayout userserviceLl;
	protected TextView userserviceTv;

	/**
	 * 温馨提示
	 */
	protected TextView detailHelp;

	/**
	 * 支付渠道信息
	 */
	protected ChargeListsResultEntity chargeIntem;

	protected CpPaymentEntity cppayment;

	protected int paymentId;

	/**
	 * 等待框
	 */
	protected Dialog dialog;
	/**
	 * 焦点事件
	 */
	private OnFocusChangeListener mOnFocusChangeListener = new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			int vid = v.getId();
		}
	};

	public PayDetailView(GuildPayActivity activity, int id) {
		this.activity = activity;
		this.paymentId = id;
	}

	@Override
	public View createView() {
		view = LayoutInflater.from(activity).inflate(
				ResUtil.getLayoutId(activity, "guild_pay_paydetail_view"),
				null);
		for (ChargeListsResultEntity charge : activity.chargeLists) {
			if (charge == null) {
				continue;
			}
			if (charge.getPaymentId() == paymentId) {
				chargeIntem = charge;
				break;
			}
		}
		cppayment = activity.cppayment;
		init();
		activityCreated();
		return view;
	}

	/**
	 * 初始化view
	 */
	private void init() {
		baseInfoLayout = (LinearLayout) view.findViewById(ResUtil.getId(
				activity, "guild_pay_baseInfo"));

		moneyLayout = (RelativeLayout) view.findViewById(ResUtil.getId(
				activity, "guild_pay_money_layout"));
		cardLayout = (LinearLayout) view.findViewById(ResUtil.getId(activity,
				"guild_pay_card_layout"));
		smsChannelLayout = (LinearLayout) view.findViewById(ResUtil.getId(
				activity, "guild_sms_channel_layout"));
		smsPayInfoLayout = (LinearLayout) view.findViewById(ResUtil.getId(
				activity, "guild_sms_pay_info_layout"));
		payConfirmLayout = (LinearLayout) view.findViewById(ResUtil.getId(
				activity, "guild_paydetail_confirm_layout"));

		// 帐号
		username = (TextView) view.findViewById(ResUtil.getId(activity,
				"guild_pay_usernameTex"));
		usernameVal = (TextView) view.findViewById(ResUtil.getId(activity,
				"guild_paydetail_username"));
		usernameVal.setText(GuildBaseInfo.getInstance().getUserName());
		// 金额
		detailMoney = (TextView) view.findViewById(ResUtil.getId(activity,
				"guild_paydetail_money"));

		// 优惠折购
		detailCardTypeEt = (TextView) view.findViewById(ResUtil.getId(activity,
				"guild_paydetail_cardtype"));
		// 实付金额
		detailMoneyTypeEt = (TextView) view.findViewById(ResUtil.getId(
				activity, "guild_paydetail_cardmoney"));

		detailConfirmBt = (Button) view.findViewById(ResUtil.getId(activity,
				"guild_paydetail_confirm"));
		userserviceLl = (LinearLayout) view.findViewById(ResUtil.getId(
				activity, "guild_paydetail_userservice_ll"));
		userserviceTv = (TextView) view.findViewById(ResUtil.getId(activity,
				"guild_paydetail_userservice_tv"));

		userserviceTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

		detailHelp = (TextView) view.findViewById(ResUtil.getId(activity,
				"guild_paydetail_help"));

		smsChannelLayout.setVisibility(View.GONE);
		smsPayInfoLayout.setVisibility(View.GONE);
		moneyLayout.setVisibility(View.VISIBLE);
		payConfirmLayout.setVisibility(View.VISIBLE);
		
		if (MoneyDiscountHelper.needDiscount(activity)) {
			cardLayout.setVisibility(View.VISIBLE);
			detailCardTypeEt.setText(SpMmkvUtils.getValue(cardLayout.getContext(), SpMmkvUtils.KEY_DISCOUNT) + "折");
			float discountMoney =
				MoneyDiscountHelper.calculateMoneyAfterDiscount(cardLayout.getContext(), cppayment.getMoney());
			detailMoneyTypeEt.setText(discountMoney + "元");
		} else {
			cardLayout.setVisibility(View.GONE);
		}
		
		this.detailMoney.setText(cppayment.getMoney() + "元");

		String userAgreementUrl = SdkBaseInfo.getUserAgreementUrl(activity
				.getBaseContext());

		if (DevEnv.LOGO == 0 || userAgreementUrl == null
				|| userAgreementUrl.length() == 0) {
			// 本地协议不会空
			userserviceLl.setVisibility(View.GONE);
		}
		userserviceLl.setVisibility(View.GONE);
	}

	protected void showfailInfo(String des) {
		PayUtil.toast(activity, des);
	}

	protected void selectAmount() {
		List<String> list = new ArrayList<String>();
		if (list.size() == 0) {
			showfailInfo(activity.getResources().getString(
					ResUtil.getStringId(activity,
							"guild_pay_amount_not_select")));
			return;
		}
		// 第二个参数是表示Dialog的风格样式，第三个是Dialog的数据，主要是尝试下怎么给一个类数据而已
		AmountDialogHelper dlg = new AmountDialogHelper(activity, list,
				detailMoney);
		dlg.show();
	}

}
